package com.base.helper;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import com.base.comm.CommUrls;
import com.base.po.jd.Token;
import com.base.utils.GsonUtil;

/**
 * 京东联盟接口
 * 
 * @author Administrator
 *
 */
public class JDHelper {
	/**
	 * TODO 获取code
	 * 
	 * 需要手动去线上获取
	 */

	/**
	 * 获取accesstoken
	 */
	public static String getJDtoken(String code) {
		String url = "https://oauth.jd.com/oauth/token?grant_type=authorization_code&client_id=" + CommUrls.JDAPPKEY
				+ "&client_secret=" + CommUrls.JDAPPSECRET + "&scope=read&redirect_uri=" + CommUrls.HOST_PATH + "&code="
				+ code;
		String json = WebHelper.PostData(url, null, null, null);
		if (json != null) {
			Token token = GsonUtil.getObject(json, Token.class);
			json = token.getAccess_token();
		}
		return json;
	}

	// private static JdClient getJdClient() {
	// if (client == null) {
	// client = new DefaultJdClient("https://api.jd.com/routerjson",
	// CommUrls.JDACCESSTOKEN, CommUrls.JDAPPKEY,
	// CommUrls.JDAPPSECRET);
	// }
	// return client;
	// }
	/**
	 * 获取商品列表
	 * 
	 * @param apikey
	 * @param Name
	 * @param OrderBy
	 * @param Page
	 * @param Category
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String getShopping(String apikey, String Name, Integer OrderBy, Integer Page, String Category)
			throws UnsupportedEncodingException {
		if (apikey == null) {
			return null;
		}
		if (Page == null) {
			Page = 0;
		}
		Integer pageSize = 20;// 二十个商品一页
		String url = "http://www.haojingke.com/index.php/api/index/myapi?type=goodslist&apikey=" + apikey
				+ "&iscoupon=1&mincommission=10&page=" + Page + "&pageSize=" + pageSize;
		if (Name != null && !Name.equals("")) {
			url += "&keyword=" + new String(Name.getBytes(StandardCharsets.UTF_8), "GBK");
		}
		if (OrderBy != null) {// 排序 0 默认 1券后价 2券金额 3佣金比例 4 佣金金额
			url += "&sort=" + OrderBy;
		}
		if (Category != null && !Category.equals("")) {
			url += "&cname=" + new String(Category.getBytes(StandardCharsets.UTF_8), "GBK");
		}
		System.out.println(url);
		String json = WebHelper.PostData(url, null, "text/html; charset=UTF-8", null);
		System.out.println(json);
		return json;
	}

	/**
	 * 获取商品详情
	 * 
	 * @param apikey
	 * @param skuid
	 * @return
	 */
	public static String getShoppingDetail(String apikey, String skuid) {
		if (apikey == null || apikey.equals("") || skuid == null || skuid.equals("")) {
			return null;
		}
		String url = "http://www.haojingke.com/index.php/api/index/myapi?type=goodsdetail&apikey=" + apikey + "&skuid="
				+ skuid;
		String json = WebHelper.PostData(url, null, "text/html; charset=UTF-8", null);
		System.out.println(json);
		return json;
	}

	/**
	 * 创建查询推广id
	 * 
	 * @param apikey
	 * @param positionName
	 * @return
	 */
	public static String createUnionId(String apikey, String positionName) {
		if (apikey == null || apikey.equals("")) {
			return null;
		}
		String url = "http://www.haojingke.com/index.php/api/index/myapi?type=jdgygetpid&apikey=" + apikey
				+ "&positionName=" + positionName + "&promotionType=" + 1;
		String json = WebHelper.PostData(url, null, "text/html; charset=UTF-8", null);
		System.out.println(json);
		return json;
	}

}